<template id="FrmFool">
    <el-card style="width:1200px;margin:20px auto">
        <div v-for="gf in sysGroupFields" v-bind:key="gf.OID">
            <template v-if="gf.CtrlType == '' || gf.CtrlType == null">
                <!--表单字段-->
                <div style="text-align: left; font-size: 22px;box-sizing: border-box;padding-left: 12px;margin-bottom: 30px">{{gf.Lab}}</div>
                <MapAttrForm :mainData="mainData" :mapAttrs="gf.MapAttrs" :frmData="frmData" :rules="rules" :readonly="isReadOnlyExt"  :ref="'childRules_'+gf.OID"></MapAttrForm>
            </template>
            <template v-if="gf.CtrlType == 'Ath' && gf.IsVisable===1" v-show="gf.show===1">
                <!--附件-->
                <!--<div style="text-align: left">{{gf.Lab}}</div>-->
                <Ath :ath="gf.Ath" :refPKVal="workID" :readonly="isReadOnlyExt" :ref="'childRules_'+gf.OID"></Ath>
            </template>
            <template v-if="gf.CtrlType == 'Dtl'"  v-show="gf.show==1">
                <!--从表-->
                <!--<div style="text-align: left;margin-top:15px">{{gf.Lab}}</div>-->
                <Dtl :dtl="gf.Dtl" :refPKVal="workID" :FK_MapData="FK_MapData" :readonly="isReadOnlyExt"></Dtl>
            </template>
            <template v-if="gf.CtrlType == 'FWC'&&node.FWCSta!=0">
                <!--审核组件-->
                <div style="text-align: left;margin-top:15px"></div>
                <WorkCheck  :isShowTagName="true"  :node="node" :isReadonly=isReadOnly ref="WorkCheckRef"></WorkCheck>
            </template>
            <template v-if="gf.CtrlType == 'SubFlow'&&node.SFSta!=0">
               <!--父子流程-->
                <div style="text-align: left;margin-top:15px">{{gf.Lab}}</div>
                <SubFlow :frmAttachment="gf.Ath" :refPKVal="workID" :isRead="isReadOnly"></SubFlow>
            </template>
        </div>
    </el-card>
</template>

<script>
    import {sendThis,GetDataTableOfTBChoice,GetCommPopData,GetFrmEleDBsByKeyOfEn,ReqDays} from '@/api/MapExt.js';
    import MapAttrForm from "../components/mapAttrForm.vue"
    import Ath from "./ath.vue";
    import Dtl from "./dtl.vue";
    import WorkCheck from "../workopt/workcheck.vue";
    import SubFlow from "../workopt/subflow.vue";
    import $ from 'jquery';
    export default {
        name: "FrmFool",
        components: {
            MapAttrForm,
            Ath,
            Dtl,
            WorkCheck,
            SubFlow,
        },
        props:{
            PFrmData:{type:Object,default:null},
            mainData:{},
            isReadOnly:{type:Number ,default:0},
            isReadOnlyExt:{type:Number,default:0}
        },
        data(){
            return{
                params:{},
                frmData:{},
                mianData:{},
                sysGroupFields:[],//分组
                mapAttrs:{},//表单字段
                rules:{},//表单字段校验规则 必填，正则表达式
                aths:[], //附件
                dtls:[], //从表
                FK_MapData:"",
                workID:0,
                node:{}

            }
        },
        mounted() {

        },
        created(){
            // 发送this 到 js 文件里
            sendThis(this);

            this.frmData = this.PFrmData;
            this.params = this.$store.getters.getData;
            this.workID = this.params.WorkID;
            this.initData();
            this.FK_MapData = this.frmData["Sys_MapData"][0].No;
            this.node = this.frmData["WF_Node"][0];
        },
        methods:{

            initData(){
                this.sysGroupFields= this.frmData.Sys_GroupField; //分组
                this.GetParseMapAttr(); //解析MapAttr的属性及扩展属性和级联
                let _this = this;
                this.aths = this.frmData.Sys_FrmAttachment;
                this.dtls = this.frmData.Sys_MapDtl;
                this.sysGroupFields.forEach(gf=>{
                    gf.show=1;
                    if(gf.CtrlType=="" || gf.CtrlType==null)
                        gf.MapAttrs=_this.mapAttrs.filter(function(mapAttr){
                            return mapAttr.GroupID==gf.OID;
                        });
                    if(gf.CtrlType=="Ath"){
                        let aths = _this.aths.filter(function(ath){
                            return ath.MyPK == gf.CtrlID && ath.IsVisable==1;
                        });
                        if(aths.length!=0){
                            gf.Ath=aths[0];
                            gf.IsVisable = 1;
                        }else {
                            gf.IsVisable = 0
                        }
                    }

                    if(gf.CtrlType=="Dtl")
                        gf.Dtl = _this.dtls.filter(function(dtl){
                            return dtl.No == gf.CtrlID;
                        })[0];
                })
                console.log(this.sysGroupFields);
            },
            /**
             * 分析字段类型和扩展属性的解析
             * @constructor
             */
            GetParseMapAttr:function(){
                let mapExts = this.GetMapExtsGroup();
                let _this = this;
                this.mapAttrs = this.frmData.Sys_MapAttr;
                let attrRules=[];
                $.each(this.mapAttrs,function(i,mapAttr){
                    attrRules=[];
                    if(_this.isReadOnlyExt ==1)
                        mapAttr.UIIsEnable = 0;
                    if(mapAttr.UIIsInput==1)
                        attrRules.push({
                            required: true,
                            message: '请输入'+mapAttr.Name,
                            trigger: 'blur'
                        })

                    if(mapExts[mapAttr.MyPK]!= undefined && mapExts[mapAttr.MyPK].length != 0)
                        mapAttr.MapExts = mapExts[mapAttr.MyPK];
                    else
                        mapAttr.MapExts=[];
                    //分析字段所属的类型
                    let AtPara = mapAttr.AtPara;
                    let isEnableJS = AtPara == "" || AtPara == null || AtPara == undefined || AtPara.indexOf('@IsEnableJS=1') == -1 ? false : true;
                    //外键或者外部数据源
                    if((mapAttr.LGType == "0" && mapAttr.MyDataType == "1" && mapAttr.UIContralType == 1)
                        || (mapAttr.LGType == "2" && mapAttr.MyDataType == "1")){
                        mapAttr.DataType="DDL";
                        mapAttr.type="select";
                        //增加下拉框
                        mapAttr.Options = _this.InitDDLData(mapAttr);
                        mapAttr.IsEnableJs = isEnableJS;
                        if(mapAttr.LGType==0)
                            mapAttr.BindKeyType=1;
                        else
                            mapAttr.BindKeyType = 0;
                        return true;
                    }
                    //枚举类型
                    let attrdefVal="";
                    if(mapAttr.LGType == 1){
                        if(mapAttr.UIContralType == 1){
                            mapAttr.DataType="DDL";
                            mapAttr.type="select";
                            mapAttr.Options = _this.InitDDLData(mapAttr);
                            mapAttr.IsEnableJs = isEnableJS;
                        }
                        if(mapAttr.UIContralType == 2){ //枚举复选框多选
                            mapAttr.DataType="CheckBox";
                            mapAttr.type="checkboxs";
                            mapAttr.Options = _this.InitDDLData(mapAttr);
                            let val = _this.mainData[mapAttr.KeyOfEn];
                            if(val==null ||val=="")
                                _this.mainData[mapAttr.KeyOfEn]=[];
                            else{
                                let vals = val.split(",");
                                _this.mainData[mapAttr.KeyOfEn]=[];
                                vals.forEach(item=>{
                                    _this.mainData[mapAttr.KeyOfEn].push(parseInt(item));
                                })
                            }

                        }
                        if(mapAttr.UIContralType == 3){ //担心按钮
                            mapAttr.DataType="Radio";
                            mapAttr.type="radio";
                            mapAttr.Options = _this.InitDDLData(mapAttr);
                            mapAttr.IsEnableJs = isEnableJS;
                        }
                        return true;
                    }
                    mapAttr.DataType="Input";
                    if (mapAttr.LGType == 0) {
                        switch (parseInt(mapAttr.MyDataType)) {
                            case 1://普通文本
                                switch (parseInt(mapAttr.UIContralType)) {
                                    case 4: //地图
                                        mapAttr.type = "Map";
                                        break;
                                    case 6://字段附件
                                        mapAttr.type = "FieldAth";
                                        break;
                                    case 8://写字板
                                        mapAttr.type = "HandWrite";
                                        break;
                                    case 9://超链接
                                        mapAttr.type = "Link";
                                        break;
                                    case 13://身份证
                                        mapAttr.type = "IDCard";
                                        break;
                                    case 16://系统定位
                                        mapAttr.type = "Fixed";
                                        break;
                                    case 18://按钮
                                        mapAttr.type = "button";
                                        break;
                                    case 50://工作进度
                                        mapAttr.type = "JobSchedule";
                                        break;
                                    case 101://评分标准
                                        mapAttr.type = "Score";
                                        break;
                                    default:
                                        //判断是不是富文本编辑器
                                        if (mapAttr.AtPara && mapAttr.AtPara.indexOf("@IsRichText=1") >= 0) {
                                            if (mapAttr.UIIsEnable == "0" || this.isReadOnlyExt == 0) {
                                                //使用div展示
                                                mapAttr.type = "div";
                                                break;
                                            }
                                            mapAttr.type = "richText";
                                            break;
                                        }
                                        //判断是不是大块文本
                                        if (mapAttr.IsSupperText == 1 || mapAttr.UIHeight > 40) {
                                            mapAttr.type = "textarea";
                                            break;
                                        }
                                        mapAttr.type = "string";
                                        if(mapAttr.MapExts.length!=0 ){
                                            $.each(mapAttr.MapExts,function(k,mapExt){
                                                if(mapAttr.UIIsEnable==0){

                                                    return true;
                                                }
                                                let PopModel = _this.GetPara(mapAttr.AtPara,"PopModel");
                                                if (PopModel != undefined && PopModel != "" && mapExt.ExtType == PopModel && PopModel != "None"){
                                                    mapAttr.type=PopModel;
                                                    let mapExt = mapAttr.MapExts[0];
                                                    //单选还是多选
                                                    let selectType = mapExt.SelectType;
                                                    selectType = selectType == null || selectType == undefined || selectType == "" ? 1 : selectType;

                                                    mapAttr.selectType = selectType==1?true:false;
                                                    if(mapAttr.type=='PopBindSFTable' || mapAttr.type=='PopBindEnum' ||mapAttr.type=='PopTableList' || mapAttr.type=='PopGroupList'){
                                                        mapAttr.Options = GetCommPopData(mapExt,PopModel);
                                                        let  val = _this.mainData[mapAttr.KeyOfEn];
                                                        if(val!=null && val!=undefined && val!="")
                                                            _this.mainData[mapAttr.KeyOfEn]=val.split(",");
                                                    }


                                                    return true;
                                                }
                                                let TBModel = _this.GetPara(mapAttr.AtPara,"TBFullCtrl");
                                                if (TBModel != undefined && TBModel != "" && TBModel != "None" && (mapExt.ExtType == "FullData")){
                                                    mapAttr.type="tbFullCtrl";
                                                    return true;
                                                }
                                                let  val = _this.mainData[mapAttr.KeyOfEn];
                                                switch (mapExt.ExtType){
                                                    case "MultipleChoiceSmall"://小范围多选
                                                        mapAttr.type="checkBoxs";
                                                        mapAttr.Options=GetDataTableOfTBChoice(mapExt, _this.frmData,_this.$store.getters.getWebUser);
                                                        if(val!=null && val!=undefined && val!="")
                                                            _this.mainData[mapAttr.KeyOfEn]=val.split(",");
                                                        break;
                                                    case "SingleChoiceSmall"://小范围单选
                                                        mapAttr.type="radio";
                                                        mapAttr.Options=GetDataTableOfTBChoice(mapExt, _this.frmData,_this.$store.getters.getWebUser);
                                                        if(val!=null && val!=undefined && val!="")
                                                            _this.mainData[mapAttr.KeyOfEn]=val.split(",");
                                                        break;
                                                    case "MultipleChoiceSearch"://搜索多选
                                                        mapAttr.type="ChoiceSearch"
                                                        mapAttr.loading = false;
                                                        mapAttr.Options=[];
                                                        if(val!=null && val!=undefined && val!=""){
                                                            _this.mainData[mapAttr.KeyOfEn]=val.split(",");
                                                            mapAttr.Options=GetFrmEleDBsByKeyOfEn(mapAttr.KeyOfEn,mapAttr.FK_MapData,_this.workID);
                                                        }

                                                        break;
                                                    case "MultipleInputSearch"://高级快速录入
                                                        mapAttr.type="InputSearch"
                                                        if(val!=null && val!=undefined && val!=""){
                                                            _this.mainData[mapAttr.KeyOfEn]=val.split(",");
                                                            mapAttr.Options=GetFrmEleDBsByKeyOfEn(mapAttr.KeyOfEn,mapAttr.FK_MapData,_this.workID);
                                                        }

                                                        break;
                                                    case "BindFunction"://绑定函数(现在只处理文本，其他的单独处理了)
                                                        break;
                                                    case "RegularExpression":
                                                        var filter = mapExt.Doc.replace(/【/g, '[').replace(/】/g, ']').replace(/（/g, '(').replace(/）/g, ')').replace(/｛/g, '{')
                                                        filter = filter.substr(1);
                                                        filter = filter.substr(0,filter.length-1);
                                                        attrRules.push({
                                                            pattern: filter,
                                                            message: mapExt.Tag1,
                                                            trigger: mapExt.Tag.substring(2)
                                                        })
                                                        break;
                                                    default:
                                                        alert(mapAttr.Name + "字段扩展属性" + mapExt.ExtType + "该类型还未解析，请反馈给开发人员");
                                                        break;
                                                }

                                            })
                                        }
                                        break;
                                }
                                break;
                            case 2://整数
                                mapAttr.DataType="Number";
                                mapAttr.type = "number";
                                mapAttr.precision = 0;
                                break;
                            case 4:  //复选框
                                mapAttr.DataType="CheckBox";
                                mapAttr.type = "checkbox";
                                mapAttr.IsEnableJs = isEnableJS;
                                break;
                            case 3://浮点
                            case 5://双精度
                                mapAttr.DataType="Float";
                                mapAttr.type = "float";
                                attrdefVal = mapAttr.DefVal;
                                if (attrdefVal != null && attrdefVal !== "" && attrdefVal.indexOf(".") >= 0)
                                    mapAttr.precision = attrdefVal.substring(attrdefVal.indexOf(".") + 1).length;
                                else
                                    mapAttr.precision = 2;
                                break;
                            case 6://日期类型
                            case 7://时间类型
                                mapAttr.DataType="Date";
                                switch(parseInt(mapAttr.IsSupperText)){ //获取日期格式
                                    case 0:
                                        mapAttr.dateFmt = "yyyy-MM-dd";
                                        mapAttr.type = "date";
                                        break;
                                    case 1:
                                        mapAttr.dateFmt = "yyyy-MM-dd HH:mm";
                                        mapAttr.type = "datetime";
                                        break;
                                    case 2:
                                        mapAttr.dateFmt = "yyyy-MM-dd HH:mm:ss";
                                        mapAttr.type = "datetime";
                                        break;
                                    case 3:
                                        mapAttr.dateFmt = "yyyy-MM";
                                        mapAttr.type = "date";
                                        break;
                                    case 4:
                                        mapAttr.dateFmt = "HH:mm";
                                        mapAttr.type = "time";
                                        break;
                                    case 5:
                                        mapAttr.dateFmt = "HH:mm:ss";
                                        mapAttr.type = "time";
                                        break;
                                    case 6:
                                        mapAttr.dateFmt = "MM-dd";
                                        mapAttr.type = "date";
                                        break;
                                }
                                break;
                            case 8://金额
                                mapAttr.DataType="Money";
                                mapAttr.type = "money";
                                attrdefVal = mapAttr.DefVal;
                                if (attrdefVal != null && attrdefVal !== "" && attrdefVal.indexOf(".") >= 0)
                                    mapAttr.precision = attrdefVal.substring(attrdefVal.indexOf(".") + 1).length;
                                else
                                    mapAttr.precision = 2;
                                break;
                        }
                        if(attrRules.length!=0){
                            _this.rules[mapAttr.KeyOfEn]=attrRules;
                        }
                    }
                });

                //处理自动计算，日期时间天数的计算
                let sys_mapExts = _this.frmData.Sys_MapExt.filter((mapExt)=>{
                    return mapExt.ExtType=="AutoFull" || mapExt.ExtType=="ReqDays";
                });
                if(sys_mapExts.length!=0){
                    sys_mapExts.forEach(mapExt=>{
                        if(mapExt.ExtType=="AutoFull"){

                        }
                        if(mapExt.ExtType=="ReqDays" && _this.mainData[mapExt.AttrOfOper]==0){
                            _this.mainData[mapExt.AttrOfOper]=ReqDays(mapExt,_this.mainData,_this);
                        }
                    })
                }
            },
            /**
             * 分解计算表达式
             * doc
             */
            calculator(doc){

            },
            InitDDLData:function(mapAttr){
                let ddlData=[];
                if(this.isReadOnly==1){
                    let val = this.mainData[mapAttr.KeyOfEn+"Text"];
                    if (val == null || val == undefined || val == "")
                        val = this.mainData[mapAttr.KeyOfEn + "T"];
                    ddlData.push({"No":this.mainData[mapAttr.KeyOfEn],"Name":val});
                    return ddlData;
                }
                let data = this.frmData[mapAttr.KeyOfEn];
                if (data == undefined)
                    data = this.frmData[mapAttr.UIBindKey];
                if (data == undefined) {
                    //枚举类型的.
                    if (mapAttr.LGType == 1) {
                        let enums = this.frmData.Sys_Enum;
                        enums.forEach(item=>{
                            if(item.EnumKey==mapAttr.UIBindKey)
                                ddlData.push({"No":item.IntKey,"Name":item.Lab});
                        });
                    }
                }else{
                    data.forEach(item=>{
                        ddlData.push({"No":item.No,"Name":item.Name});
                    })
                }
                return ddlData;
            },
            validateForm(){
                //必填，正则表达式的校验
                let flag=true;
                let filedFlag = true;
                let _this = this;
                _this.sysGroupFields.forEach(gf=>{
                    if(gf.CtrlType == '' || gf.CtrlType == null){
                         filedFlag = _this.$refs[`childRules_${gf.OID}`][0].validateForm();
                        if(filedFlag==false){
                            this.$alert(gf.Name+"分组中信息填写不完整，请继续填写完整");
                            flag=false;
                        }

                    }
                    //最小上传附件的限制
                    if(gf.CtrlType=="Ath"){
                        let aths = _this.aths.filter(function(ath){
                            return ath.MyPK == gf.CtrlID && ath.IsVisable==1;
                        });
                        if(aths.length!=0){
                            let filedFlag = _this.$refs[`childRules_${gf.OID}`][0].LimitMinOfAth();
                            if(filedFlag==false)
                                flag=false;
                        }
                    }

                })

                //附件

                //审核组件
                let childVue = this.$refs.WorkCheckRef;
                if(childVue!=null){
                    filedFlag = childVue[0].WorkCheck_Save();
                    if(filedFlag==false)
                        flag=false;
                }

               return flag;

            },
            /**
             * 扩展字段根据字段MyPK分组
             * @constructor
             */
            GetMapExtsGroup:function(){
                let map = {};
                let mypk = "";
                if(this.frmData.Sys_MapExt.length==0)
                    return map;
                //对mapExt进行分组，根据AttrOfOper
                this.frmData.Sys_MapExt.forEach(function(mapExt){
                    //不是操作字段不解析
                    if (mapExt.AttrOfOper == "")
                        return true;
                    if (mapExt.ExtType == "DtlImp"
                        || mapExt.MyPK.indexOf(mapExt.FK_MapData + '_Table') >= 0
                        || mapExt.MyPK.indexOf('PageLoadFull') >= 0
                        || mapExt.ExtType == 'StartFlow'
                        || mapExt.ExtType == 'AutoFullDLL'
                        || mapExt.ExtType == 'ActiveDDLSearchCond'
                        || mapExt.ExtType == 'AutoFullDLLSearchCond')
                        return true;

                    mypk = mapExt.FK_MapData + "_" + mapExt.AttrOfOper;
                    if (!map[mypk])
                        map[mypk] = [mapExt];
                    else
                        map[mypk].push(mapExt);
                    if(mapExt.ExtType=="AutoFull" && mapExt.Doc!=null && mapExt.Doc!=""){
                        let doc = mapExt.Doc;
                        let targets = [];
                        let index = -1;
                        for (let i = 0; i < doc.length; i++) {	// 对于复杂表达式需要重点测试
                            let c = doc.charAt(i);
                            if (c == "(") {
                                index++;
                            } else if (c == ")") {
                                targets.push(doc.substring(index + 1, i));
                                i++;
                                index = i;
                            } else if (/[\+\-|*\/]/.test(c)) {
                                targets.push(doc.substring(index + 1, i));
                                index = i;
                            }
                        }
                        if (index + 1 < doc.length) {
                            targets.push(doc.substring(index + 1, doc.length));
                        }
                        targets.forEach(item=>{
                            if(item.indexOf("@")!=-1){
                                mypk =  mapExt.FK_MapData + "_" +item.replace("@","");
                                if (!map[mypk])
                                    map[mypk] = [mapExt];
                                else
                                    map[mypk].push(mapExt);
                            }
                        })
                    }
                    if(mapExt.ExtType=="ReqDays"){
                        mypk =  mapExt.FK_MapData + "_"+mapExt.Tag1;
                        if (!map[mypk])
                            map[mypk] = [mapExt];
                        else
                            map[mypk].push(mapExt);
                        mypk =  mapExt.FK_MapData + "_"+mapExt.Tag2;
                        if (!map[mypk])
                            map[mypk] = [mapExt];
                        else
                            map[mypk].push(mapExt);
                    }
                })
                let res = [];
                Object.keys(map).forEach(key => {
                    res.push({
                        attrKey: key,
                        data: map[key],
                    })
                });
                return map;
            },
        },

    }
</script>

<style scoped>

</style>
